//
//  BVCouponViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/4.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVCouponViewController.h"
#import "BVCouponCell.h"

#import "BVLoginoutMyCoupon.h"
#import "BVEmptyMyCoupon.h"
#import "BVLoginViewController.h"

#import "BVMineTool.h"
#import "BVMyCouponModel.h"

#define  COUPON_COUPONCELL_IDENTIFIER @"BVCouponCell"

@interface BVCouponViewController ()<UITableViewDelegate,UITableViewDataSource,BVCouponCellDelegate>

@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)UISegmentedControl *segmented;
@property (nonatomic,strong)BVLoginoutMyCoupon *loginView;
@property (nonatomic,strong)BVEmptyMyCoupon *emptyCoupon;
@property (nonatomic,strong)BVMineTool *mineTool;
@property (nonatomic,strong)NSMutableArray *arr;
@end

@implementation BVCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的优惠券";
    self.view.backgroundColor = [UIColor BVBackgroundLightGrayColor];
    [self chooseChildView];
    
}
-(UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, ScreenW, ScreenH-64-50) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor BVBackgroundLightGrayColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 120;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        [_tableView registerClass:[BVCouponCell class] forCellReuseIdentifier:COUPON_COUPONCELL_IDENTIFIER];
    }
    return _tableView;
}
-(UISegmentedControl *)segmented{
    
    if (!_segmented) {
        _segmented = [[UISegmentedControl alloc]initWithItems:@[@"未使用",@"已使用",@"已过期"]];
        _segmented.frame = CGRectMake(30, 10, ScreenW-60, 30);
        _segmented.layer.borderColor = [UIColor BVBaseOrangeColor].CGColor;
        _segmented.layer.borderWidth = 1;
        _segmented.layer.cornerRadius = 6;
        _segmented.clipsToBounds = YES;
        _segmented.backgroundColor = [UIColor clearColor];
        _segmented.selectedSegmentIndex = 0;
        _segmented.tintColor= [UIColor BVBaseOrangeColor];
        [_segmented addTarget:self action:@selector(myAction:)
                 forControlEvents:UIControlEventValueChanged];
    }
    return _segmented;
}

- (void)chooseChildView{
    
    if (!self.user.login) {
        self.loginView = [[BVLoginoutMyCoupon alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
        [self.view addSubview:self.loginView];
        __weak __typeof(self) weakSelf = self;
        self.loginView.logoutBlock = ^(){
            BVLoginViewController * login = [[BVLoginViewController alloc]init];
            [weakSelf.navigationController pushViewController:login animated:YES];
        };
        
    }else{
        [self.loginView removeFromSuperview];
        [self setUI];
    }

}
-(void)setUI{
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.segmented];
    [self myAction:self.segmented];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BVMyCouponModel *myCouponModel = self.arr[indexPath.row];
    
     BVCouponCell * cell = [tableView dequeueReusableCellWithIdentifier:COUPON_COUPONCELL_IDENTIFIER];
    cell.indexPath = indexPath;
    cell.myCouponModel = myCouponModel;
    cell.delegate = self;
    
    if (_segmented.selectedSegmentIndex == 0) {
        cell.BgImg.image = [UIImage imageNamed:@"couponW"];
        cell.typeImg.hidden = YES;
        cell.typeLbel.text = @"点击使用";
    }
    else if (_segmented.selectedSegmentIndex == 1){
        cell.BgImg.image = [UIImage imageNamed:@"couponY"];
        cell.typeImg.hidden = YES;
        cell.typeLbel.text = @"已使用";
    }
    else{
        cell.BgImg.image = [UIImage imageNamed:@"couponY"];
        cell.typeImg.hidden = NO;
        cell.typeLbel.text = @"已过期";
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (_segmented.selectedSegmentIndex == 0) {
        NSLog(@"未使用状态");
    }else if (_segmented.selectedSegmentIndex == 1){
        [self showMessage:@"优惠券已过期!"];
        NSLog(@"已过期状态");
    }else if (_segmented.selectedSegmentIndex ==2){
        [self showMessage:@"优惠券已使用!"];
        NSLog(@"已使用状态");
    }
}

-(void)myAction:(UISegmentedControl *)seg{
//    [self showLoading];
    [self.arr removeAllObjects];
    NSString *indexStr = [NSString stringWithFormat:@"%ld",(long)seg.selectedSegmentIndex];
    [self.mineTool fetchUserCoupon:indexStr success:^(NSMutableArray * arr) {
//        [self hideHUD];
        [self.arr addObjectsFromArray:arr];
        [self.tableView reloadData];
        [self layoutView:self.arr];
    } failure:^(NSError *error) {
//        [self hideHUD];
        [self showMessage:@"未知错误"];
        [self.tableView reloadData];
    }];
    
}

#pragma mark - deleteButtonDelegate
-(void)getIndex:(NSIndexPath*)indexPath{
    
    NSLog(@"这是代理传过来的~~~%ld",(long)indexPath.row);
}
- (void)layoutView:(NSMutableArray *)arr{
    if (arr.count == 0) {
        self.emptyCoupon = [[BVEmptyMyCoupon alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
        [self.tableView addSubview:self.emptyCoupon];
        
    }else{
        [self.emptyCoupon removeFromSuperview];
    }
}
-(NSMutableArray *)arr{
    if (!_arr) {
        _arr = [NSMutableArray array];
    }
    return _arr;
}
-(BVMineTool *)mineTool{
    if (!_mineTool) {
        _mineTool = [[BVMineTool alloc]init];
    }
    return _mineTool;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
